import axios from 'axios'
import Vue from 'vue'

export default {
  namespaced: true,
  state: () => ({
    token: '',
    userInfo: {}
  }),
  mutations: {
    getToken (state, token) {
      state.token = token
    },
    updateUserInfo (state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    async getUserInfo (ctx) {
      // console.log(ctx.state.token)
      const { data: res } = await axios.get('/my/userinfo', {
        headers: {
          Authorization: ctx.state.token
        }
      })
      // console.log(res)
      if (res.code !== 0) return Vue.prototype.$message.error(res.message)
      Vue.prototype.$message.success(res.message)
      ctx.commit('updateUserInfo', res.data)
    }
  }
}
